var request = require('../../utils/request.js')
var app = getApp();
Page({
  data: {
    showd: false,
    list: [],
    area_id:null,
    charge_card:null,
    gaodelist: [],
    postList: {},
    sign:true,
    locationlist:null,
    mobile:""
  },
  onShow() {
    my.getNetworkType({
      success: (res) => {
        if(!res.networkAvailable||res.networkType=='UNKNOWN'){
          my.showToast({
            content: '网络状态不佳，请检查网络',
            type:'fail',
            duration: 2000
          })
        }
      }
    })
    this.charge_cards();
    this.getuserinfo();
  },
  onLoad: function () {
  },
  // 弹出框
  activity_click() {
    var that = this;
    that.setData({
      showd: !that.data.showd
    })
  },
  //获取充值套餐
  charge_cards() {
    const res = my.getStorageSync({ key: 'locationlist' });
    if(res){
      this.data.locationlist=res.data;
      this.data.area_id = res.data.area_id;
      this.setData({
        locationlist:this.data.locationlist
      })
      let success = (res) => {
        if(res.length!=0){
          this.data.list = res
          for (var i = 0; i < this.data.list.length; i++) {
            this.data.list[i].price = (this.data.list[i].money - this.data.list[i].discount).toFixed(2)
          }
          this.setData({
            list: this.data.list
          })
        }else{
          my.showToast({
            content: '该地区无月卡套餐',
            type:'fail',
            duration: 2000
          })
        }
       
      }
      request.getdata("/charge_cardlist", {area_id:this.data.area_id}, success, this);
    }else{
      my.showToast({
        content: '获取信息失败，请重试',
        type:'fail',
        duration: 2000
      })
    }
  },
  //购买月卡
  getMonthlyCard: function (e) {
    if(this.data.mobile){
      this.data.postList.source = 1;
      this.data.postList.charge_card_id = e.currentTarget.dataset.item.id;
      if (this.data.charge_card){//月卡是否存在
        const current_time= new Date();
        const currentTime = current_time.getTime()//当前时间转换成秒
        const expire = new Date(this.data.charge_card.expireAt.replace(/-/g, "/"));
        const expireTime = expire.getTime()//过期时间转换成秒
        if (currentTime < expireTime){//月卡是否过期
          my.showToast({
            content: '月卡未过期,无法购买月卡',
            type:'fail',
            duration: 2000
          })
          return
        }
      }
      if (this.data.sign==true){
        this.pay()
        this.data.sign = false;
        setTimeout(() => {
          this.data.sign = true
        }, 10000)
      }else{
        return;
      }
    }else{
      my.confirm({
        title: '温馨提示',
        content: '还没有绑定手机哦!',
        confirmButtonText: '马上绑定',
        cancelButtonText: '暂不需要',
        success: (result) => {
          if(result.confirm){
            my.navigateTo({
              url: '/pages/user/mobile'
            })
          }else if (result.cancel) {
          }
          
        },
      });
    }
  },
  pay:function(){
    this.data.postList.area_id=this.data.area_id
      let success=(param)=>{
        if(param == "nomoney"){
          my.redirectTo({
            url: '/pages/user/wallet'
          });
        }else{
          my.tradePay({
          orderStr: param.callback, 
          success: (res) => {
            if (res.resultCode==9000) {
              my.redirectTo({
                url:'/pages/user/wallet'
              })
              my.showToast({
                content:'支付成功',
                type:'success',
                duration: 1000
              })
            }
          },
          fail: (res) => {
            
          }
          });
        }
      }
    request.postdata('/charge_card/buy',this.data.postList,success,this)
  },
  //获取页面数据
  getuserinfo() {
    let success = (res) => {
      if(res.telephone){
        this.data.mobile = res.telephone;
        this.data.charge_card = res.charge_card
      }
    }
    request.getdata("/users/user/show", {}, success, this);
  }
})